package com.equipmentRent.system.service.impl;

import java.util.List;

import com.equipmentRent.common.core.domain.entity.SysUser;
import com.equipmentRent.common.utils.DateUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.equipmentRent.common.utils.ShiroUtils;
import com.equipmentRent.system.domain.VO.CategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.equipmentRent.system.mapper.CategoryMapper;
import com.equipmentRent.system.domain.Category;
import com.equipmentRent.system.service.ICategoryService;
import com.equipmentRent.common.core.text.Convert;

import static org.apache.commons.lang3.SystemUtils.getUserName;

/**
 * 分类Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-15
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements ICategoryService

{
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询分类
     * 
     * @param categoryId 分类主键
     * @return 分类
     */
    @Override
    public Category selectCategoryByCategoryId(Long categoryId)
    {
        return categoryMapper.selectCategoryByCategoryId(categoryId);
    }

    /**
     * 查询分类列表
     * 
     * @param category 分类
     * @return 分类
     */
    @Override
    public List<Category> selectCategoryList(Category category)
    {
        return categoryMapper.selectCategoryList(category);
    }

    /**
     * 新增分类
     * 
     * @param category 分类
     * @return 结果
     */
    @Override
    public int insertCategory(Category category)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        category.setCreateTime(DateUtils.getNowDate());
        category.setCreateBy(sysUser.getUserName());
        category.setUpdateBy(sysUser.getUserName());
        category.setUpdateTime(DateUtils.getNowDate());
        return categoryMapper.insertCategory(category);
    }

    /**
     * 修改分类
     * 
     * @param category 分类
     * @return 结果
     */
    @Override
    public int updateCategory(Category category)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        category.setUpdateTime(DateUtils.getNowDate());
        category.setUpdateBy(sysUser.getUserName());
        return categoryMapper.updateCategory(category);
    }

    /**
     * 批量删除分类
     * 
     * @param categoryIds 需要删除的分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByCategoryIds(String categoryIds)
    {
        return categoryMapper.deleteCategoryByCategoryIds(Convert.toStrArray(categoryIds));
    }

    /**
     * 批量软删除分类
     *
     * @param categoryIds 需要软删除的分类主键
     * @return 结果
     */
    @Override
    public int softDeleteCategoryByCategoryIds(String categoryIds)
    {
        String[] strArray = Convert.toStrArray(categoryIds);
        int count = 0;
        for (String s : strArray) {
            Category category = categoryMapper.selectCategoryByCategoryId(Long.valueOf(s));
            int i = categoryMapper.update(null, Wrappers.<Category>lambdaUpdate()
                    .set(Category::getIsDelete, 1L)
                    .eq(Category::getCategoryId, category.getCategoryId()));
            count = count + i;
        }
        return count;
    }

    /**
     * 删除分类信息
     * 
     * @param categoryId 分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByCategoryId(Long categoryId)
    {
        return categoryMapper.deleteCategoryByCategoryId(categoryId);
    }

    @Override
    public List<CategoryVo> getCategoryList() {
        return categoryMapper.getCategoryList();
    }
}
